import requests

# Home Assistant的URL和API令牌
HASS_URL='http://192.168.123.253:8123'
ACCESS_TOKEN='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJiOWJhMDM0YjYyODQ0MGQ2YmYxNmMyM2Q2NzkyN2RiYyIsImlhdCI6MTcyNzMzNjE2NCwiZXhwIjoyMDQyNjk2MTY0fQ.umXZPcX7FbPZ6EOIIgBPXh5rtgbbEfOaLbuiQoGdBKM'


# 风扇的实体ID，可以在Home Assistant中查找
ENTITY_ID = 'cover.dmaker_p9_2068_cover'


# 构建请求的headers
headers = {
    'Authorization': f'Bearer {ACCESS_TOKEN}',
    'Content-Type': 'application/json'
}
        
# 打开风扇
def set_cover_on():
	url = f'{HASS_URL}/api/services/cover/turn_on'
	payload = {
		"entity_id": ENTITY_ID
	}
	response = requests.post(url, json=payload, headers=headers)
	print(response.text)


# 关闭风扇
def set_cover_off():
	url = f'{HASS_URL}/api/services/cover/turn_off'
	payload = {
		"entity_id": ENTITY_ID
	}
	response = requests.post(url, json=payload, headers=headers)
	print(response.text)


# 风速控制
def set_cover_speed(speed):
	url = f'{HASS_URL}/api/services/cover/set_speed'
	payload = {
		"entity_id": ENTITY_ID,
		"speed": speed
	}
	response = requests.post(url, json=payload, headers=headers)
	print(response.text)


# 摇头控制
def oscillate_cover(oscillating):
	url = f'{HASS_URL}/api/services/cover/oscillate'
	payload = {
		"entity_id": ENTITY_ID,
		"oscillating": oscillating
	}
	response = requests.post(url, json=payload, headers=headers)
	print(response.text)

# 设置风速百分比
def set_cover_percentage(percentage):
	url = f'{HASS_URL}/api/services/cover/set_percentage'
	payload = {
		"entity_id": ENTITY_ID,
		"percentage": percentage
	}
	response = requests.post(url, json=payload, headers=headers)
	print(response.text)

# 设置指示灯状态
def set_led_state(state):
	url = f'{HASS_URL}/api/services/cover/set_led'
	payload = {
		"entity_id": ENTITY_ID,
		"state": state
	}
	response = requests.post(url, json=payload, headers=headers)
	print(response.text)
                


if __name__ == "__main__":
    set_cover_on()
	set_fan_speed(3)        # 设置风速为3
    oscillate_fan(True)     # 开启摇头
    set_fan_percentage(50)  # 设置风速百分比为50%
    set_led_state(False)    # 关闭指示灯
	set_led_state(True)     # 开启指示灯
	set_cover_off()